/* Copyright - Apache License 2.0
 * 
 * The project "kyou" is
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *      http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.kyou.data.dquery;

import net.kyou.exception.KyouErr;
import net.kyou.exception.KyouException;

/**
 * DQuery段中的约束条件
 * 
 * @author NuclearG (<a href="mailto:nuclearg@163.com">nuclearg@163.com</a>)
 */
class DQueryRestriction {
    /**
     * 约束的类型<li>true - 一元约束</li><li>false - 双元约束</li>
     */
    final boolean simple;

    /**
     * 一元约束的类型
     */
    final SimpleRestriction simpleType;
    /**
     * 当一元约束类型为ARRAYINDEX时，该值表示数组下标的值
     */
    final int arrayIndex;

    /**
     * 双元约束的属性名称
     */
    final String name;
    /**
     * 双元约束的关系<li>true - 等于</li><li>false - 不等于</li>
     */
    final boolean eq;
    /**
     * 双元约束的属性值
     */
    final String value;

    /**
     * 解析restriction字符串
     */
    DQueryRestriction(String restriction) {
        if (restriction.contains("=")) {
            // 两元约束
            this.simple = false;
            this.simpleType = null;
            this.arrayIndex = -1;

            if (restriction.contains("!=")) {
                int pos = restriction.indexOf("!=");
                this.name = restriction.substring(0, pos);
                this.eq = false;
                this.value = restriction.substring(pos + 2);
            } else {
                int pos = restriction.indexOf('=');
                this.name = restriction.substring(0, pos);
                this.eq = true;
                this.value = restriction.substring(pos + 1);
            }
        } else {
            // 一元约束
            this.simple = true;
            this.name = null;
            this.eq = false;
            this.value = null;

            if (restriction.equals("FIELD")) {
                this.simpleType = SimpleRestriction.FIELD;
                this.arrayIndex = -1;
            } else if (restriction.equals("STRUCT")) {
                this.simpleType = SimpleRestriction.STRUCT;
                this.arrayIndex = -1;
            } else if (restriction.equals("*")) {
                this.simpleType = SimpleRestriction.WILDCHAR;
                this.arrayIndex = -1;
            } else
                try {
                    this.simpleType = SimpleRestriction.ARRAYINDEX;
                    this.arrayIndex = Integer.parseInt(restriction);
                    if (this.arrayIndex < 0)
                        throw new NumberFormatException();
                } catch (NumberFormatException ex) {
                    throw new KyouException(KyouErr.Unknown);
                }

        }
    }

    enum SimpleRestriction {
        FIELD, STRUCT, ARRAYINDEX, WILDCHAR
    }
}
